<template>
  <div id="app">
    <Dogs
      v-for="(item, index) in list"
      :key="index"
      :picAddress="item.dogImgUrl"
      :name="item.dogName"
      @AddLoves="adlove(item, $event)"
    />
    <!-- @love="loves(item, $event)" -->
    <p>你喜欢的狗子:</p>
    <p
      v-for="item in loveDogs"
      :key="item.value"
      @click="delLove(item)"
    >
    {{ item }}</p>
  </div>
</template>

<script>
import Dogs from './components/Dogs'
export default {
  data () {
    return {
      list: [
        {
          dogImgUrl:
            'http://nwzimg.wezhan.cn/contents/sitefiles2029/10146688/images/21129958.jpg',
          dogName: '博美'
        },
        {
          dogImgUrl:
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1224576619,1307855467&fm=26&gp=0.jpg',
          dogName: '泰迪'
        },
        {
          dogImgUrl:
            'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2967740259,1518632757&fm=26&gp=0.jpg',
          dogName: '金毛'
        },
        {
          dogImgUrl:
            'https://pic1.zhimg.com/80/v2-7ba4342e6fedb9c5f3726eb0888867da_1440w.jpg?source=1940ef5c',
          dogName: '哈士奇'
        }
      ],
      loveDogs: []
    }
  },
  methods: {
    adlove (item, name) {
      // console.log(item)
      // console.log(name)
      // 思路，通过数组的findindex找到对应name属性的索引值，若存在索引不为-1，则添加；反之则不添加
      // const i = this.loveDogs.indexOf(name)
      // console.log(i)
      if (this.loveDogs.indexOf(name) === -1) this.loveDogs.push(name)
    },
    delLove (item) {
      // console.log(item)
      const i = this.loveDogs.indexOf(item)
      // console.log(i)
      this.loveDogs.splice(i, 1)
    }
  },
  components: {
    Dogs
  }
}
</script>

<style lang="less" scoped></style>
